package com.example.autorefreshbussines.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {
    @Autowired
    private ConfigDemo2 configDemo2;

    @Bean
    @RefreshScope  // 关键：保证刷新后重新生成bean
    public DataSource dataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(configDemo2.getUrl());
        dataSource.setUsername(configDemo2.getUsername());
        dataSource.setPassword(configDemo2.getPassword());
        return dataSource;
    }
}
